import { Button, Tag, Switch, Tooltip } from "antd";
import { EditOutlined, DeleteOutlined, KeyOutlined } from "@ant-design/icons";

const checkDisabled = (dict_type: string) => {
  if (dict_type === "sys_user_role") {
    return true;
  }
  return false;
};

/**
 * 系统配置表格列定义
 */
export function sysSettingTableColumns(editAction: any, deleteAction: any) {
  return [
    {
      title: "ID",
      dataIndex: "id",
      key: "id",
      width: 80,
      hidden: false,
    },
    {
      title: "配置键",
      dataIndex: "config_key",
      key: "config_key",
      width: 200,
      ellipsis: true,
      render: (text: string) => (
        <Tooltip title={text}>
          <span className="font-mono text-sm">{text}</span>
        </Tooltip>
      ),
    },
    {
      title: "配置名称",
      dataIndex: "config_name",
      key: "config_name",
      width: 150,
      ellipsis: true,
    },
    {
      title: "配置类型",
      dataIndex: "config_type",
      key: "config_type",
      width: 120,
      render: (type: string) => {
        const typeMap: { [key: string]: { label: string; color: string } } = {
          system_basic: { label: "系统基础", color: "blue" },
          email: { label: "邮件配置", color: "green" },
          sms: { label: "短信配置", color: "orange" },
          file_storage: { label: "文件存储", color: "purple" },
          security: { label: "安全配置", color: "red" },
          interface: { label: "接口配置", color: "cyan" },
          cache: { label: "缓存配置", color: "geekblue" },
          ui: { label: "界面配置", color: "magenta" },
        };
        const config = typeMap[type] || { label: type, color: "default" };
        return <Tag color={config.color}>{config.label}</Tag>;
      },
    },
    {
      title: "配置值",
      dataIndex: "config_value",
      key: "config_value",
      width: 200,
      ellipsis: true,
      render: (value: string, record: any) => {
        const isEncrypted = record.is_encrypted === 1;
        return (
          <div className="flex items-center gap-2">
            {isEncrypted && <KeyOutlined className="text-amber-500" />}
            <Tooltip title={value}>
              <span className={`${isEncrypted ? 'text-amber-600' : ''} font-mono text-sm`}>
                {value}
              </span>
            </Tooltip>
          </div>
        );
      },
    },
    {
      title: "是否加密",
      dataIndex: "is_encrypted",
      key: "is_encrypted",
      width: 100,
      render: (encrypted: number) => (
        <Tag color={encrypted === 1 ? "orange" : "default"}>
          {encrypted === 1 ? "已加密" : "明文"}
        </Tag>
      ),
    },
    {
      title: "排序",
      dataIndex: "sort_order",
      key: "sort_order",
      width: 80,
      render: (order: number) => (
        <span className="font-mono">{order}</span>
      ),
    },
    {
      title: "描述",
      dataIndex: "description",
      key: "description",
      width: 200,
      ellipsis: true,
      render: (text: string) => (
        <Tooltip title={text}>
          <span>{text || "-"}</span>
        </Tooltip>
      ),
    },
    {
      title: "更新时间",
      dataIndex: "update_time",
      key: "update_time",
      width: 160,
      render: (time: string) => (
        <span className="font-mono text-sm">{time}</span>
      ),
    },
    {
      title: "操作",
      key: "action",
      width: 150,
      fixed: "right" as const,
      render: (record: any) => (
        <div className="flex gap-2">
          <Button
            type="primary"
            size="small"
            icon={<EditOutlined />}
            onClick={() => editAction("edit", record)}
          >
            编辑
          </Button>
          <Button
            danger
            size="small"
            icon={<DeleteOutlined />}
            onClick={() => deleteAction(record)}
          >
            删除
          </Button>
        </div>
      ),
    },
  ];
}

/**
 * 配置类型选项
 */
export const configTypeOptions = [
  { label: "系统基础配置", value: "system_basic" },
  { label: "邮件配置", value: "email" },
  { label: "短信配置", value: "sms" },
  { label: "文件存储配置", value: "file_storage" },
  { label: "安全配置", value: "security" },
  { label: "接口配置", value: "interface" },
  { label: "缓存配置", value: "cache" },
  { label: "界面配置", value: "ui" },
];

/**
 * 表单验证规则
 */
export const formRules = {
  config_key: [
    { required: true, message: "请输入配置键" },
    { pattern: /^[a-zA-Z0-9_]+$/, message: "配置键只能包含字母、数字和下划线" },
    { max: 100, message: "配置键长度不能超过100个字符" },
  ],
  config_name: [
    { required: true, message: "请输入配置名称" },
    { max: 200, message: "配置名称长度不能超过200个字符" },
  ],
  config_type: [
    { required: true, message: "请选择配置类型" },
  ],
  config_value: [
    { max: 2000, message: "配置值长度不能超过2000个字符" },
  ],
  description: [
    { max: 500, message: "描述长度不能超过500个字符" },
  ],
  sort_order: [
    { type: "number" as const, min: 0, max: 9999, message: "排序值必须在0-9999之间" },
  ],
};

/**
 * 默认配置模板
 */
export const defaultConfigTemplates = {
  system_basic: [
    {
      config_key: "system_name",
      config_name: "系统名称",
      config_value: "养老机构消防一体化管理指挥中心",
      description: "系统显示名称",
    },
    {
      config_key: "system_logo",
      config_name: "系统Logo",
      config_value: "/logo.png",
      description: "系统Logo图片路径",
    },
    {
      config_key: "copyright_info",
      config_name: "版权信息",
      config_value: "© 2024 养老机构消防一体化管理指挥中心",
      description: "系统版权信息",
    },
    {
      config_key: "contact_phone",
      config_name: "联系电话",
      config_value: "400-123-4567",
      description: "系统客服联系电话",
    },
  ],
  email: [
    {
      config_key: "email_smtp_host",
      config_name: "SMTP服务器",
      config_value: "smtp.example.com",
      description: "邮件SMTP服务器地址",
    },
    {
      config_key: "email_smtp_port",
      config_name: "SMTP端口",
      config_value: "587",
      description: "邮件SMTP服务器端口",
    },
    {
      config_key: "email_username",
      config_name: "邮箱用户名",
      config_value: "admin@example.com",
      description: "发送邮件的用户名",
    },
    {
      config_key: "email_password",
      config_name: "邮箱密码",
      config_value: "",
      description: "发送邮件的密码",
      is_encrypted: 1,
    },
  ],
  security: [
    {
      config_key: "session_timeout",
      config_name: "会话超时时间(秒)",
      config_value: "7200",
      description: "用户会话超时时间",
    },
    {
      config_key: "password_min_length",
      config_name: "密码最小长度",
      config_value: "6",
      description: "用户密码最小长度要求",
    },
    {
      config_key: "login_max_attempts",
      config_name: "最大登录尝试次数",
      config_value: "5",
      description: "用户登录失败最大尝试次数",
    },
  ],
  file_storage: [
    {
      config_key: "file_max_size",
      config_name: "文件最大大小(MB)",
      config_value: "50",
      description: "单个文件最大上传大小",
    },
    {
      config_key: "file_allowed_types",
      config_name: "允许的文件类型",
      config_value: "jpg,jpeg,png,gif,pdf,doc,docx,xls,xlsx",
      description: "允许上传的文件类型",
    },
  ],
};

/**
 * 获取配置类型标签颜色
 */
export function getConfigTypeColor(type: string): string {
  const colorMap: { [key: string]: string } = {
    system_basic: "blue",
    email: "green",
    sms: "orange",
    file_storage: "purple",
    security: "red",
    interface: "cyan",
    cache: "geekblue",
    ui: "magenta",
  };
  return colorMap[type] || "default";
}

/**
 * 格式化配置值显示
 */
export function formatConfigValue(value: string, type: string): string {
  if (!value) return "-";
  
  // 对于密码类型，显示遮盖
  if (type === "password" || value === "***已加密***") {
    return "***已加密***";
  }
  
  // 对于过长的值，进行截断
  if (value.length > 50) {
    return value.substring(0, 50) + "...";
  }
  
  return value;
}
